package cn.itcast.xuesheng.web.servlet;

import cn.itcast.xuesheng.domain.User;
import cn.itcast.xuesheng.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//@WebServlet("/user/*")
@Controller
@RequestMapping("/user")
@SessionAttributes("user")
public class UserServlet {
    private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

    //    @Autowired
//    private UserService userService;
//    protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    @RequestMapping("/register")
    public @ResponseBody
    Map register(User user) {
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("application/json;charset=utf-8");
//        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = new HashMap<>();
//        User user = new User();
//        try {
//            BeanUtils.populate(user, parameterMap);
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        }
        //Spring解耦：
        UserService userService = (UserService) ac.getBean("userService");
        User check = userService.check(user.getUsername());
        if (check == null) {
            userService.insert(user);
            map.put("register", false);
            map.put("message", "注册成功");
        } else {
            map.put("register", true);
            map.put("message", "注册失败");
        }
//        writeValue(response, map);
        return map;
    }

    @RequestMapping("/login")
    public @ResponseBody
    Map login(Model model, User user, String checkCode, ModelMap modelMap, HttpServletRequest request) {
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("application/json;charset=utf-8");
//        Map<String, String[]> parameterMap = request.getParameterMap();
//        String checkCode = request.getParameter("checkCode");
//        HttpSession httpSession = request.getSession();
        String checkcode_session = (String) request.getSession().getAttribute("checkcode_session");
//         String checkcode_session = (String) modelMap.get("checkcode_session");
//        httpSession.removeAttribute("checkcode_session");
        Map<String, Object> map = new HashMap<>();
        if (checkcode_session != null && checkcode_session.equalsIgnoreCase(checkCode)) {
//            User LoginUser = new User();
//            try {
//                BeanUtils.populate(LoginUser, parameterMap);
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            } catch (InvocationTargetException e) {
//                e.printStackTrace();
//            }
            UserService userService = (UserService) ac.getBean("userService");
            System.out.println(userService);
            User loginuser = userService.isLogin(user);
            if (loginuser == null) {
                map.put("Tcheck", 1);
                map.put("msg", "用户名或密码出错");
            } else {
                map.put("Tcheck", 2);
                map.put("msg", "用户名密码正确");
                model.addAttribute("user", user);
            }
        } else {
            map.put("Tcheck", 0);
            map.put("msg", "验证码出错");
        }
        return map;
    }

    @RequestMapping("/check")
    public @ResponseBody
    Map check(String username) {
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("text/html;charset=utf-8");
//        String username = request.getParameter("username");
        Map<String, Object> map = new HashMap<>();
//        UserServiceImpl userService=new UserServiceImpl();
        UserService userService = (UserService) ac.getBean("userService");
        User checkuser = userService.check(username);
        if (checkuser == null) {
            map.put("checkUser", true);
            map.put("msg", "用户名可用");
        } else {
            map.put("checkUser", false);
            map.put("msg", "用户名太受欢迎了,请换一个");
        }
//        writeValue(response, map);
        return map;
    }

    @RequestMapping("/find")
    public @ResponseBody
    User find(ModelMap modelMap) {
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("application/json;charset=utf-8");
//        Object user = request.getSession().getAttribute("user");
//        writeValue(response, user);
        User user = (User) modelMap.get("user");
        return user;
    }

    @RequestMapping("/quit")
    public void quit(SessionStatus sessionStatus, HttpServletResponse response, HttpServletRequest request) throws IOException {
//        request.getSession().invalidate();
//        response.sendRedirect(request.getContextPath() + "/Login.html");
        sessionStatus.setComplete();
        response.sendRedirect(request.getContextPath() + "/Login.html");
    }

    @RequestMapping("/modify")
    public @ResponseBody
    User modify(ModelMap modelMap) {
//        User user = (User) request.getSession().getAttribute("user");
        User user = (User) modelMap.get("user");
//           writeValue(response, user);
        return user;
    }

    @RequestMapping("/update")
    public @ResponseBody
    Map update(@RequestBody User user) {
//        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = new HashMap<>();
//        User user = new User();
//        try {
//            BeanUtils.populate(user, parameterMap);
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        }
        UserService userService = (UserService) ac.getBean("userService");
        userService.update(user);
        map.put("register", false);
//        writeValue(response, map);
        return map;
    }

    @RequestMapping("/deleteMsg")
    public void deleteMsg(String tid) {
//        String tid = request.getParameter("tid");
        UserService userService = (UserService) ac.getBean("userService");
        userService.delete(tid);
    }

    @RequestMapping("/deleteAllMsg")
    public void deleteAllMsg(String[] uid) {
//        String[] id = request.getParameterValues("uid");
        UserService userService = (UserService) ac.getBean("userService");
        userService.deleteSelect(uid);
    }
}
